package com.sixkey.controller.app;

import cn.dev33.satoken.stp.StpUtil;
import com.sixkey.entity.User;
import com.sixkey.response.Response;
import com.sixkey.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * ClassName: ApiLoginController
 * Package: com.sixkey.controller.api
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2023/10/8 - 15:39
 * @Version: v1.0
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/login")
public class ApiLoginController {

    private final IUserService userService;

    /**
     * 用户使用手机号登录
     * @param
     * @return
     */
    @GetMapping("/phone/login/{phone}")
    public Response phoneLogin(@PathVariable("phone") String phone){
        log.info("用户使用手机号登录请求进来~{}",phone);
        Map<String,Object> map = userService.apiPhoneDoLogin(phone);
        return Response.success(map);
    }

    /**
     * 获取用户信息
     * @param
     * @return
     */
    @GetMapping("/info/{phone}")
    public Response getUserInfo(@PathVariable("phone") String phone){
        log.info("用户使用手机号登录请求进来~{}",phone);
        User user = userService.getUserInfo(phone);
        return Response.success(user);
    }

    /**
     * 用户退出
     * @param
     * @return
     */
    @PostMapping("/logout/{userId}")
    public Response logout(@PathVariable("userId") Integer userId){
        log.info("用户退出请求进来~");
        StpUtil.logout(userId);
        return Response.success();
    }
}
